﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //第1种方式
            var sqlCon = "server = BF-202003151143\\sqlexpress; database = Ppz; uid = sa; pwd = 3578520r";
            SqlConnection sqlConnection = new SqlConnection(sqlCon);

            sqlConnection.Open();

            //try:存放可能会出现bug的代码
            //try
            //{

            //    //添加SQL语句
            //    //String sql = "insert A(commodityId,commodityName)values(7,'da');";

            //    //修改改SQL语句
            //    // String sql = "update commodity set commodityName='ss'where id=4;";

            //    //删除SQL语句
            //    // String sql = "delete from commodity where commodityId = 4;";

            //    //SqlCommand com = new SqlCommand(sql, sqlConnection);
            //   // int r = com.ExecuteNonQuery();
            //   // Console.WriteLine(r + "行受影响");



            //}

            ////catch的作用：捕获异常
            //catch (Exception ex)
            //{
            //    Console.WriteLine("出错了：" + ex.Message);
            //}
            ////finally的作用：无论报对还是报错，代码最终都会被执行！
            //finally
            //{
            //    //    关闭连接
            //    sqlConnection.Close();
            //}

            SqlCommand sqlCommand = new SqlCommand("select * from commodity", sqlConnection);
            var reade = sqlCommand.ExecuteReader();

            Console.WriteLine("{0}\t\t{1}\t{2}\t{3}\t{4}\t{5}", "商品ID",
                "商品名", "价格", "库存", "厂家ID", "类型ID");
            while (reade.Read())
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
                    reade["commodityId"], reade["commodityName"], reade["price"],
                    reade["inventory"],reade["FactoryId"], reade["genreId"]);
            }

            sqlConnection.Close();
            //第2种方式            
            //Data Source = 服务器名称 \ 数据库实例名; Initial Catalog = 数据库名称; User ID = 用户名; Password = 密码


        }
    }
}
